package cn.exrick.xboot.modules.content.serviceimpl.mybatis;

import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.config.custom.CustomPage;
import cn.exrick.xboot.modules.content.dao.*;
import cn.exrick.xboot.modules.content.dao.mapper.HontUserMapper;
import cn.exrick.xboot.modules.content.dao.mapper.HontUserRoleMapper;
import cn.exrick.xboot.modules.content.entity.*;
import cn.exrick.xboot.modules.content.entity.HontVo.HontUserQueryVo;
import cn.exrick.xboot.modules.content.entity.HontVo.HontUserRolelevelQueryVo;
import cn.exrick.xboot.modules.content.entity.HontVo.HontUserRolelevelVo;
import cn.exrick.xboot.modules.content.service.HontDepartmentHeaderService;
import cn.exrick.xboot.modules.content.service.mybatis.IHontUserService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.opencv.presets.opencv_core;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 权限接口实现
 * @author Exrick
 */
@Slf4j
@Service
@Transactional
public class IHontUserServiceImpl extends ServiceImpl<HontUserMapper, HontUser> implements IHontUserService {

    @Autowired
    private HontUserMapper hontUserMapper;

    @Autowired
    private HontDepartmentDao departmentDao;

    @Autowired
    private HontDepartmentHeaderDao hontDepartmentHeaderDao;

    @Autowired
    private HontUserRoleDao hontUserRoleDao;

    @Autowired
    private HontRoleDao hontRoleDao;

    @Autowired
    private HontPostDao hontPostDao;
    @Autowired
    private HontUserPostDao hontUserPostDao;

    /**
     * @param hontUser
     * @param roleId
     * @param page
     * @return
     */
    @Override
    public CustomPage selectAllListByHontUser(HontUser hontUser, String roleId, CustomPage page) {
        HontUserQueryVo hontUserQueryVo = new HontUserQueryVo();
        hontUserQueryVo.setDepartmentId(hontUser.getDepartmentId());
        hontUserQueryVo.setPostId(hontUser.getPostId());
        hontUserQueryVo.setNickName(hontUser.getNickName().trim());
        hontUserQueryVo.setRoleId(roleId);
        hontUserQueryVo.setFirstRow(page.getFirstRow());
        hontUserQueryVo.setLastRow(page.getPageSize());
        List<Map<String, Object>> allListByHontUser = hontUserMapper.findAllListByHontUser(hontUserQueryVo);

        List<HontUser> users = new ArrayList<>();
        for (int i = 0 ; i < allListByHontUser.size(); i++) {
            //获取部门名称
            String hontUserId = String.valueOf(allListByHontUser.get(i).get("id"));
            String departmentId1 = String.valueOf(allListByHontUser.get(i).get("departmentId"));
            if (StrUtil.isNotBlank(departmentId1)){
                HontDepartment departmentId = departmentDao.findById(departmentId1).orElse(null);
                if (departmentId != null){
                    allListByHontUser.get(i).put("departmentTitle",departmentId.getTitle());
                }
            }

            //拥有权限
            List<HontDepartmentHeader> byHontUserId = hontDepartmentHeaderDao.findByHontUserId(hontUserId);
            if (byHontUserId.size()>0){
                List<String> hontDepartments = new ArrayList<>();
                for (int j = 0; j<byHontUserId.size(); j++){
                    HontDepartment hontDepartment = departmentDao.findById(byHontUserId.get(j).getHontDepartmentId()).orElse(null);

                    hontDepartments.add(hontDepartment.getTitle());
                }
                List<String> collect = hontDepartments.stream().distinct().collect(Collectors.toList());
                allListByHontUser.get(i).put("departmentIds",collect);

            } else {
                allListByHontUser.get(i).put("departmentIds",new ArrayList<>());
            }

            //岗位
            List<HontUserPost> byHontUserPost = hontUserPostDao.findByHontUserId(hontUserId);
            if (byHontUserPost.size() > 0) {
                List<String> postIds = new ArrayList<>();
                List<String> postIdNames = new ArrayList<>();
                for (int p = 0; p < byHontUserPost.size(); p++) {
                    HontPost hontPost = hontPostDao.findById(byHontUserPost.get(p).getHontPostId()).orElse(null);
                    if(hontPost!=null){
                        postIds.add(hontPost.getId());
                        postIdNames.add(hontPost.getTitle());
                    }
                }
                allListByHontUser.get(i).put("hontPostIds",postIds);
                allListByHontUser.get(i).put("hontPostNames",postIdNames);
            }else {
                allListByHontUser.get(i).put("hontPostIds",new ArrayList<>());
                allListByHontUser.get(i).put("hontPostNames",new ArrayList<>());
            }
            //角色

            if (StrUtil.isNotBlank(hontUserId)) {
                HontUserRole byHontUser = hontUserRoleDao.findByHontUserId(hontUserId);
                if (byHontUser !=null){
                    HontRole hontRole = hontRoleDao.findById(byHontUser.getHontRoleId()).orElse(null);
                    if (hontRole != null) {
                        allListByHontUser.get(i).put("roles",hontRole.getName());
                    }
                }
            }

        }
        List<Map> allListByHontUserCount = hontUserMapper.findAllListByHontUserCount(hontUserQueryVo);
        page.setResultData(allListByHontUser);
        page.setTotalRows(Long.valueOf(allListByHontUserCount.size()));
        return page;
    }

    @Override
    public List<Map<String, Object>> getAssessmentTeam() {
        return hontUserMapper.getAssessmentTeam();
    }

    @Override
    public  List<HontUserRolelevelVo> getHontUserAndRoleAndLevel(HontUserRolelevelQueryVo hontUserRolelevelQueryVo) {
        List<HontUserRolelevelVo> hontUserRolelevelVos=
                hontUserMapper.getHontUserAndRoleAndLevel(hontUserRolelevelQueryVo);
        return hontUserRolelevelVos;
    }

    @Override
    public List<HontUserRolelevelVo> getUserByDepartAndRole(HontUserRolelevelQueryVo hontUserRolelevelQueryVo) {
        List<HontUserRolelevelVo> hontUserRolelevelVos=
                hontUserMapper.getUserByDepartAndRole(hontUserRolelevelQueryVo);
        return hontUserRolelevelVos;
    }

    @Override
    public Integer getUserRoleCountByDep(String departmentId,String roleId) {
        return hontUserMapper.getUserRoleCountByDep(departmentId,roleId);
    }
    @Override
    public HontUser login(String userName) {
        QueryWrapper<HontUser> wrapper=new QueryWrapper();
        wrapper.lambda().and(tmp ->tmp.eq(HontUser::getUserName, userName).or().eq(HontUser::getMobile, userName));
        List<HontUser> hontUsers = hontUserMapper.selectList(wrapper);
        if(hontUsers.size()>0){
            return hontUsers.get(0);
        }
        return null;
    }

    @Override
    public Result<Object> authorizationBackground(String hontUserId, Integer isBackstage) {
        HontUser byId = this.getById(hontUserId);
        byId.setIsBackstage(isBackstage);
        this.updateById(byId);
        if(isBackstage.intValue()==0){
            return ResultUtil.success("取消登录后台权限");
        }else if(isBackstage.intValue()==1){
            return  ResultUtil.success("授权登录后台权限");
        }
        return  ResultUtil.error("参数未定义！");
    }

    @Override
    public List<HontUser> selectAllByUserName(List<String> userNames) {
        return hontUserMapper.selectAllByUserName(userNames);
    }
}